package com.weishuang.monitor.employee.jobhandler.impl;

import com.weishuang.monitor.employee.commons.SendUtils;
import com.weishuang.monitor.employee.entity.ServersBaseInfo;
import com.weishuang.monitor.employee.jobhandler.ServerHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.*;
import oshi.software.os.*;

import java.text.SimpleDateFormat;
import java.util.Date;


public class ServerBaseInfoHandler implements ServerHandler {

    public final static Logger logger = LoggerFactory.getLogger(ServerBaseInfoHandler.class);

    private static final String upServerBaseInfoUrl = "/api/serversBaseInfo/upBaseInfo";
    /**
     * 服务器信息获取
     */
    @Override
    public void run() {
        logger.debug("服务器信息获取");
        SystemInfo si = new SystemInfo();
        OperatingSystem operatingSystem = si.getOperatingSystem();
        HardwareAbstractionLayer hal = si.getHardware();

        ServersBaseInfo serversBaseInfo = new ServersBaseInfo();
        // 系统类型
        serversBaseInfo.setSystemType(operatingSystem.getFamily());
        //系统版本
        serversBaseInfo.setSystemVersion(operatingSystem.getVersionInfo().toString());
        //启动时间
        serversBaseInfo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(operatingSystem.getSystemBootTime() * 1000)));
        //cpu名称
        CentralProcessor processor = hal.getProcessor();
        CentralProcessor.ProcessorIdentifier processorIdentifier = processor.getProcessorIdentifier();
        serversBaseInfo.setCpuInfo(processorIdentifier.getName());
        //物理cpu数
        int physicalPackageCount = processor.getPhysicalPackageCount();
        serversBaseInfo.setCpuPhysicalNum(physicalPackageCount);
        //核心数
        int physicalProcessorCount = processor.getPhysicalProcessorCount();
        serversBaseInfo.setCpuPhysicalCoreNum(physicalProcessorCount);
        //逻辑cpu数
        int logicalProcessorCount = processor.getLogicalProcessorCount();
        serversBaseInfo.setCpuLogicalCoreNum(logicalProcessorCount);

        try {
            SendUtils.sendRequest(upServerBaseInfoUrl,serversBaseInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }


//        GlobalMemory memory = hal.getMemory();
//        //内存总量
//        String s1 = FormatUtil.formatBytes(memory.getTotal());
//        //已用内存
//        String s2 = FormatUtil.formatBytes(memory.getAvailable());
//
//        FileSystem fileSystem = operatingSystem.getFileSystem();
//        List<OSFileStore> fileStores = fileSystem.getFileStores();
//
//        long diskStoresTotal = 0;
//        long useDiskStoresTotal = 0;
//
//        for (OSFileStore osFileStore : fileStores) {
//            diskStoresTotal += osFileStore.getTotalSpace();
//            useDiskStoresTotal += osFileStore.getUsableSpace();
//        }
//        System.out.println("总量/已用：" + diskStoresTotal + "/" + useDiskStoresTotal);
    }
}
